var groupContrllers = angular.module('groupContrllers', []);

groupContrllers.controller('GroupCtrl',['$scope','$http',
                                            
  function($scope,$http){
	$http.get('/mvc/group/listdata').success(function(data){
		$scope.GroupList=data;
	});
	
	$scope.formData={};
	
	$scope.newGroup = function(){
		$scope.edit_title = "New User Group: ";
		$scope.isEditing = true;
		$scope.isSet = false;
		$scope.formData={};
	};
	
	$scope.edit = function(group){
		$scope.edit_title = "Edit User Group: ";
		$scope.isEditing = true;
		$scope.isSet = false;
		$scope.formData.id = group.id;
		$scope.formData.name = group.name;
		$scope.formData.nurseryId = group.nurseryId;
	};
	
	$scope.showpri = function(group){
		
		$scope.currGroupId = group.id;
		
		var param = {};
		param.groupId = group.id;
		
		
		$http(
				{
					method:'POST',
					url:'/mvc/privilege/list',
					data:$.param(param),
					headers : {'Content-Type':'application/x-www-form-urlencoded'}
				}).success(
					function(data, status, headers, config){
						$scope.PriList=data.privileges;
						$scope.isSet = true;
						$scope.isEditing = false;
					}
				);
	};
	
	$scope.add = function(group){
		
		if(group.name == ""){
			alert("Please input the user group name.");
			return;
		}
		
		$http(
		{
			method:'POST',
			url:'/mvc/group/add',
			data:$.param(group),
			headers : {'Content-Type':'application/x-www-form-urlencoded'}
		}).success(
			function(data, status, headers, config){
				$scope.GroupList=data;
				$scope.isEditing = true;
				$scope.isSet = false;
			}
		);
	};
	
	$scope.update = function(group){
		
		if(group.name == ""){
			alert("Please input the department name.");
			return;
		}
		
		$http(
		{
			method:'POST',
			url:'/mvc/group/update',
			data:$.param(group),
			headers : {'Content-Type':'application/x-www-form-urlencoded'}
		}).success(
			function(data, status, headers, config){
				$scope.GroupList=data;
				$scope.isEditing = true;
				$scope.isSet = false;
			}
		);
	};
	
	
	$scope.remove = function(group){
		if(confirm("Do you really want to delete ?")){
			
			$scope.formData.id = group.id;
			$scope.formData.name = group.name;
			$scope.formData.nurseryId = group.nurseryId;
			
			$http({
				method:'POST',
				url:'/mvc/group/delete',
				data:$.param($scope.formData),
				headers : {'Content-Type':'application/x-www-form-urlencoded'}
			}).success(
				function(data, status, headers, config){
					$scope.GroupList=data;
					$scope.isEditing = false;
					$scope.isSet = false;
				}
			);
		}
	};
	
	$scope.finishSet = function(){
		$scope.isSet = false;
	};
	
	$scope.toggle = function (pri) {  
		
		var param= {};
		param.groupId = $scope.currGroupId;
		param.privilegeCode = pri.code;
		param.checked = !pri.selected;
		
		$http({
			method:'POST',
			url:'/mvc/privilege/set',
			data:$.param(param),
			headers : {'Content-Type':'application/x-www-form-urlencoded'}
		}).success(
			function(data, status, headers, config){
				$scope.PriList=data.privileges;
			}
		);
		
    };
}]);